package com.fone.player.play.adapter;

import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fone.player.ApplicationManage;
import com.fone.player.R;
import com.fone.player.client.DramaRst;
import com.fone.player.client.DramaRst.Cnt;
import com.fone.player.storage.StorageModule;
import com.fone.player.util.FoneUtil;

/**
 * 剧集GridView item的adapter
 * 
 * @author kongxiaojun
 * @since 2014-4-21
 */
public class GridSeriesItemAdapter extends BaseAdapter {

	private List<DramaRst.Cnt> cntList;
	private String currentPlayUrl;
	private String currentPlayCId;

	public GridSeriesItemAdapter(List<DramaRst.Cnt> cntList, String currentPlayUrl) {
		super();
		this.cntList = cntList;
		this.currentPlayUrl = currentPlayUrl;
		this.currentPlayCId = FoneUtil.getCIdByUrl(currentPlayUrl);
	}

	@Override
	public int getCount() {
		if (cntList == null) {
			return 0;
		}
		return cntList.size();
	}

	@Override
	public Object getItem(int position) {
		return cntList.get(position);

	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		DramaRst.Cnt cnt = cntList.get(position);
		long cid = 0;
		try {
			cid = Long.valueOf(FoneUtil.getCIdByUrl(cnt.url));
		} catch (Exception e) {
			e.printStackTrace();
		}
		ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(ApplicationManage.getAplicationManageInstance()).inflate(R.layout.video_series_item, null);
			holder = new ViewHolder((TextView) convertView.findViewById(R.id.video_series_item_name), (ImageView) convertView.findViewById(R.id.cache_grid_status_iv));
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (cid != 0 && currentPlayCId != null && currentPlayCId.equals(cid + "")) {// 正在看
			convertView.setBackgroundResource(R.color.video_series_item_bg_watching);
			holder.seriesName.setTextColor(ApplicationManage.getAplicationManageInstance().getResources().getColor(R.color.white));
			holder.status.setImageBitmap(null);
		} else {
			if (cid != 0 && StorageModule.getInstance().getPlayRecordByCID(cid) != null) {// 已看过
				convertView.setBackgroundResource(R.drawable.video_detail_series_cached_shape);
				holder.seriesName.setTextColor(ApplicationManage.getAplicationManageInstance().getResources().getColor(R.color.video_title_bar_title_textcolor));
				holder.status.setImageBitmap(null);
			} else {// 未看过
				convertView.setBackgroundResource(R.drawable.video_detail_series_normal_shape);
				holder.seriesName.setTextColor(ApplicationManage.getAplicationManageInstance().getResources().getColor(R.color.video_title_bar_title_textcolor));
				holder.status.setImageBitmap(null);
			}
		}
		if (cid != 0 && StorageModule.getInstance().isOfflineCacheFileByCID(cid)) {// 已缓存
			holder.seriesName.setTextColor(ApplicationManage.getAplicationManageInstance().getResources().getColor(R.color.cache_item_green_color));
			holder.status.setImageResource(R.drawable.detail_download_finished);
		}

		holder.seriesName.setText(cnt.name);
		convertView.setTag(holder);

		return convertView;

	}

	class ViewHolder {
		public TextView seriesName;
		public ImageView status;

		public ViewHolder(TextView seriesName, ImageView status) {
			super();
			this.seriesName = seriesName;
			this.status = status;
		}
	}

	public void setData(List<Cnt> cntList2, String xyzPlayUrl) {
		this.cntList = cntList2;
		this.currentPlayUrl = xyzPlayUrl;
		this.currentPlayCId = FoneUtil.getCIdByUrl(currentPlayUrl);
		notifyDataSetChanged();
	}

	public void setCurrentPlayUrl(String xyzPlayUrl) {
		String cid = FoneUtil.getCIdByUrl(xyzPlayUrl);
		if (!TextUtils.isEmpty(cid) && !cid.equals(currentPlayCId)) {
			this.currentPlayUrl = xyzPlayUrl;
			this.currentPlayCId = cid;
			notifyDataSetChanged();
		}
	}

	public String getCurrentPlayUrl() {
		return currentPlayUrl;
	}

	public String getCurrentPlayCId() {
		return currentPlayCId;
	}

}
